﻿using System;
using System.IO;

using Holdem.Interfaces.Configuration;
using Holdem.Interfaces.ReplayEngine;
using Holdem.Interfaces.Service;

namespace Holdem.ReplayEngine
{
    public class HandHistoryReplayEngineProvider : IReplayEngineProvider
    {
        private const string DEFAULT_HISTORY_SAVE_DIRECTORY = "/HoldemHandHistory";
        private readonly string _generateFileName = "HandHistory_" + DateTime.Now.ToString("yyyy_MM_dd") + ".xml";
		private readonly IConfigurationService _configurationService;
		private IReplayEngineConfiguration _replayConfiguration;

        private string HandHistorySaveDirectory
        {
            get;
            set;
        }

		public HandHistoryReplayEngineProvider(
			IConfigurationService configurationService,
			IReplayEngineConfiguration replayConfiguration)
        {
			if (configurationService == null)
				throw new ArgumentNullException("configurationService");
			if (configurationService.PersistentConfiguration == null)
				throw new ArgumentException("IConfigurationService.PersistentConfiguration");
			if (replayConfiguration == null)
				throw new ArgumentNullException("replayConfiguration");

			_configurationService = configurationService;
			_replayConfiguration = replayConfiguration;

			if (!configurationService.PersistentConfiguration.ContainsKey(ConfigKey.HandHistorySaveDirectory))
				configurationService.PersistentConfiguration.Add(ConfigKey.HandHistorySaveDirectory, DEFAULT_HISTORY_SAVE_DIRECTORY);

			var historySaveDirectory = configurationService.PersistentConfiguration.Cast<string>(ConfigKey.HandHistorySaveDirectory);

            var saveDir = historySaveDirectory ?? DEFAULT_HISTORY_SAVE_DIRECTORY;

            //Re-save the directory as a precaution.
            this.HandHistorySaveDirectory = saveDir;

            if (!configurationService.IsolatedStorage.DirectoryExists(this.HandHistorySaveDirectory))
				configurationService.IsolatedStorage.CreateDirectory(this.HandHistorySaveDirectory);
        }

        #region Implementation of IReplayEngineProvider

        public IReplayEngineConfiguration Configuration
        {
            get
            {
                return _replayConfiguration;
            }
        }

        public void Configure(IReplayEngineConfiguration configuration)
        {
            if (configuration == null)
                return;

            _replayConfiguration = configuration;
        }

        public Stream WriteableStream()
        {
            var saveFileName = this.HandHistorySaveDirectory + "/" + _generateFileName;

            var stream = 
                _configurationService.IsolatedStorage.OpenFile(
                    saveFileName, FileMode.Create, FileAccess.Write);

            return stream as Stream;
        }

        public Stream ReadableStream()
        {
            var saveFileName = this.HandHistorySaveDirectory + "/" + _generateFileName;

            var stream =
                _configurationService.IsolatedStorage.OpenFile(
                    saveFileName, FileMode.OpenOrCreate, FileAccess.Read);

            return stream as Stream;
        }

        #endregion
    }
}
